package com.muh.util;

import co.elastic.clients.elasticsearch._types.aggregations.CardinalityAggregate;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregation;
import org.springframework.data.elasticsearch.core.SearchHits;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Constent {

    /**
     * 提取聚合结果到Map中
     */
    public static Map<String, Object> extractAggregationResults(SearchHits<?> searchHits) {
        Map<String, Object> result = new HashMap<>();
        ArrayList aggregations = (ArrayList) searchHits.getAggregations().aggregations();
//        ElasticsearchAggregation elasticsearchAggregation = (ElasticsearchAggregation) aggregations1.get(0);
//        org.springframework.data.elasticsearch.client.elc.Aggregation aggregation = elasticsearchAggregation.aggregation();
//        String name = aggregation.getName();
//        Aggregate aggregate = aggregation.getAggregate();
//        Object o = aggregate._get();
//        ExtendedStatsAggregate o1 = (ExtendedStatsAggregate) o;

        // 获取顶级聚合
        aggregations.forEach(aggContainer -> {
            ElasticsearchAggregation aggContainer1 = (ElasticsearchAggregation) aggContainer;
            String aggName = aggContainer1.aggregation().getName();
//            String aggName = aggContainer.getName();
//            Object agg = aggContainer1.aggregation().getAggregate()._custom();
            Object agg = aggContainer1.aggregation().getAggregate()._get();

            // 处理不同类型的聚合结果
            if (agg instanceof co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) {
                result.put(aggName, processStringTerms((co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate) agg));
            }
            else if (agg instanceof co.elastic.clients.elasticsearch._types.aggregations.LongTermsAggregate) {
                result.put(aggName, processLongTerms((co.elastic.clients.elasticsearch._types.aggregations.LongTermsAggregate) agg));
            }
            else if (agg instanceof co.elastic.clients.elasticsearch._types.aggregations.ExtendedStatsAggregate) {
                result.put(aggName, processExtendedStats((co.elastic.clients.elasticsearch._types.aggregations.ExtendedStatsAggregate) agg));
            }
            else if (agg instanceof co.elastic.clients.elasticsearch._types.aggregations.StatsAggregate) {
                result.put(aggName, processStats((co.elastic.clients.elasticsearch._types.aggregations.StatsAggregate) agg));
            }
            //去重
            else if (agg instanceof co.elastic.clients.elasticsearch._types.aggregations.CardinalityAggregate) {
                result.put(aggName, processCardinality((co.elastic.clients.elasticsearch._types.aggregations.CardinalityAggregate) agg));
            }
            // 添加其他聚合类型的处理...
        });

        return result;
    }

    private static Object processCardinality(CardinalityAggregate stats) {
        Map<String, Object> result = new HashMap<>();
        result.put("value", stats.value());
        return result;
    }

    /**
     * 处理字符串词条聚合结果
     */
    public static Map<String, Object> processStringTerms(co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate terms) {
        Map<String, Object> result = new HashMap<>();
        result.put("doc_count", terms.sumOtherDocCount());
        terms.buckets().array().forEach(bucket -> {
            result.put(bucket.key().stringValue(), bucket.docCount());
//            System.out.println(bucket.key().stringValue()+"<>"+bucket.docCount());
        });
        return result;
    }

    /**
     * 处理扩展统计聚合结果
     */
    public static Map<String, Object> processExtendedStats(co.elastic.clients.elasticsearch._types.aggregations.ExtendedStatsAggregate stats) {
        Map<String, Object> result = new HashMap<>();
        result.put("count", stats.count());
        result.put("min", stats.min());
        result.put("max", stats.max());
        result.put("avg", stats.avg());
        result.put("sum", stats.sum());
        result.put("variance", stats.variance());
        result.put("std_deviation", stats.stdDeviation());
        return result;
    }

    /**
     * 处理基本统计聚合结果
     */
    public static Map<String, Object> processStats(co.elastic.clients.elasticsearch._types.aggregations.StatsAggregate stats) {
        Map<String, Object> result = new HashMap<>();
        result.put("count", stats.count());
        result.put("min", stats.min());
        result.put("max", stats.max());
        result.put("avg", stats.avg());
        result.put("sum", stats.sum());
        return result;
    }

    /**
     * 处理长整型词条聚合结果
     */
    public static Map<String, Object> processLongTerms(co.elastic.clients.elasticsearch._types.aggregations.LongTermsAggregate terms) {
        Map<String, Object> result = new HashMap<>();
        result.put("doc_count", terms.sumOtherDocCount());
        terms.buckets().array().forEach(bucket -> {
            result.put(String.valueOf(bucket.key()), bucket.docCount());
        });
        return result;
    }
}
